﻿#region

using System;
using System.Web;
using System.Web.Caching;

#endregion

namespace BTL.Technology.Framework.Caching
{
    //TODO : Move the to cache assembley
    public class CachingProvider
    {
        private static Cache _objCache;

        protected static Cache Cache
        {
            get
            {
                //create singleton of the cache object
                if (_objCache == null)
                {
                    _objCache = HttpRuntime.Cache;
                }
                return _objCache;
            }
        }

        public static CachingProvider Instance()
        {
            return new CachingProvider();
        }


        public virtual object GetItem(string cacheKey)
        {
            return Cache[cacheKey];
        }

        public virtual void Insert(string cacheKey, object value, CacheDependency objDependency,
                                   DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                                   CacheItemRemovedCallback onRemoveCallback)
        {
            //TODO: Add configuration settings
            if (objDependency == null)
            {
                Cache.Insert(cacheKey, value, null, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
            }
            else
            {
                //TODO: dependecy
                //Cache.Insert(CacheKey, Value, objDependency.SystemCacheDependency, AbsoluteExpiration, SlidingExpiration, Priority, OnRemoveCallback);
            }
        }


        internal void RemoveCache(string key)
        {
            Cache.Remove(key);
        }
    }
}